package com.niit.reduceJoin;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Date:2024/11/12
 * Author：Ys
 * Description:
 */
public class ReduceJionReducer extends Reducer<Text,Text,Text,Text> {
    //用来存放 商品编号 商品名称
    List<String> goodsList = new ArrayList<String>();
    //用来存放 订单编号 订单号
    List<String> orderList = new ArrayList<String>();

    Text outValue = new Text();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        // 1. 遍历values，得到订单号和商品编号
        for (Text value : values){
            if(value.toString().startsWith("goods#")){
                String[] goods = value.toString().split("#");
                String goodsInfo = goods[1];
                goodsList.add(goodsInfo);
            }
            if (value.toString().startsWith("order#")){
                String[] order = value.toString().split("#");
                String orderInfo = order[1];
                orderList.add(orderInfo);
            }
        }
        //获取2个集合的长度
        int goodsSize = goodsList.size();
        int orderSize = orderList.size();
        for(int i = 0;i<orderSize;i++){
            for (int j = 0; j < goodsSize; j++){
                outValue.set(orderList.get(i)+"\t"+goodsList.get(j));
                //最终输出：商品id 订单编号 实际支付价格 商品编号 商品名称
                context.write(key,outValue);
            }

        }
        goodsList.clear();
        orderList.clear();
    }
}
